package cn.iocoder.yudao.module.nmkj.service.tagarticle;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.nmkj.controller.admin.tagarticle.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.tagarticle.TagArticleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nmkj.dal.mysql.tagarticle.TagArticleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TAG_ARTICLE_NOT_EXISTS;

/**
 * 标签文章 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TagArticleServiceImpl implements TagArticleService {

    @Resource
    private TagArticleMapper tagArticleMapper;

    @Override
    public Long createTagArticle(TagArticleSaveReqVO createReqVO) {
        // 插入
        TagArticleDO tagArticle = BeanUtils.toBean(createReqVO, TagArticleDO.class);
        tagArticleMapper.insert(tagArticle);
        // 返回
        return tagArticle.getId();
    }

    @Override
    public void updateTagArticle(TagArticleSaveReqVO updateReqVO) {
        // 校验存在
        validateTagArticleExists(updateReqVO.getId());
        // 更新
        TagArticleDO updateObj = BeanUtils.toBean(updateReqVO, TagArticleDO.class);
        tagArticleMapper.updateById(updateObj);
    }

    @Override
    public void deleteTagArticle(Long id) {
        // 校验存在
        validateTagArticleExists(id);
        // 删除
        tagArticleMapper.deleteById(id);
    }

    private void validateTagArticleExists(Long id) {
        if (tagArticleMapper.selectById(id) == null) {
            throw exception(TAG_ARTICLE_NOT_EXISTS);
        }
    }

    @Override
    public TagArticleDO getTagArticle(Long id) {
        return tagArticleMapper.selectById(id);
    }

    @Override
    public PageResult<TagArticleDO> getTagArticlePage(TagArticlePageReqVO pageReqVO) {
        return tagArticleMapper.selectPage(pageReqVO);
    }

    @Override
    public TagArticleDO getTagArticleApp(Long id) {
        return tagArticleMapper.selectAppOne(id);
    }

}